﻿<?php
class SE {
	static function referer($ref = false)
	{
	    $SeReferer = (is_string($ref) ? $ref : $_SERVER['HTTP_REFERER']);
	    if( //Check against Google, Yahoo, MSN, Ask and others
	        preg_match(
	        "/[&\?](q|p|w|searchfor|as_q|as_epq|s|query)=([^&]+)/i",
	        $SeReferer,$pcs)
	    ){
	        if(preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
	            $SeDomain    = trim(strtolower($SeDomain[1]));
	            $SeQuery    = $pcs[2];
	            if(preg_match("/[&\?](start|b|first|stq)=([0-9]*)/i",$SeReferer,$pcs)){
	                $SePos    = (int)trim($pcs[2]);
	            }
	        }
	    }
	    if(!isset($SeQuery)){
	        if( //Check against DogPile
	            preg_match(
	            "/\/search\/web\/([^\/]+)\//i",
	            $SeReferer,$pcs)
	        ){
	            if(preg_match("/https?:\/\/([^\/]+)\//i",$SeReferer,$SeDomain)){
	                $SeDomain    = trim(strtolower($SeDomain[1]));
	                $SeQuery    = $pcs[1];
	            }
	        }
	    }
	    // We Do Not have a query
	    if(!isset($SeQuery)){ return false; }
	    $OldQ=$SeQuery;
	    $SeQuery=urldecode($SeQuery);
	    // The Multiple URLDecode Trick to fix DogPile %XXXX Encodes
	    while($SeQuery != $OldQ){
	        $OldQ=$SeQuery; $SeQuery=urldecode($SeQuery);
	    }
	    //-- We have a query
	    return array(
	        "Se"=>$SeDomain,
	        "Query"=>$SeQuery,
	        //"Pos"=>(int)$SePos,
	        "Referer"=>$SeReferer
	    );
	}
}
?>